/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Class Utilities.
 * @author Adam
 * @version $Id: ClassUtils.java 16 2009-04-12 05:05:02Z a.ruggles $
 * 
 * Created on Feb 5, 2009 at 9:24:37 PM 
 */
public final class ClassUtils {
    /**
     * The <code>Logger</code> is used by the application to generate a log messages.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(ClassUtils.class);

    /**
     * Returns a class loader.
     * @return A class loader.
     */
	public static ClassLoader getClassLoader() {
		ClassLoader loader = null;
		try {
			loader = Thread.currentThread().getContextClassLoader();
		} catch (Exception exception) {
			LOGGER.debug("Unable to get the class loader from the thread.");
		}
		if (loader == null) {
			loader = ClassUtils.class.getClassLoader();
			LOGGER.debug("Loading from ClassUtils.class.getClassLoader.");
		}
		return loader;
	}

    /**
	 * Instantiates a new object by it's class name.
	 * @param className The fully qualified class name.
	 * @return A New Object for the given class name or null.
	 */
	public static Object getObjectByName(final String className) {
		Object value = null;
		try {
			final Class<?> objectClass = Class.forName(className, true, getClassLoader());
			value = objectClass.newInstance();
		} catch (ClassNotFoundException e) {
			LOGGER.error("Unable to load DAO Module [Class Not Found]", e);
		} catch (InstantiationException e) {
			LOGGER.error("Unable to load DAO Module [Instantiation Exception]", e);
		} catch (IllegalAccessException e) {
			LOGGER.error("Unable to load DAO Module [Illegal Access]", e);
		}
		return value;
	}

	/**
     * Private Constructor.
     */
    private ClassUtils() { }
}
